package com.androidvizlab.bachelor;

import com.androidvizlab.bachelor.activities.EditOptionFileActivity;
import com.androidvizlab.bachelor.activities.ViewResultDataActivity;
import com.androidvizlab.bachelor.utilities.NumberConverter;
import com.androidvizlab.bachelor.utilities.VizLabPreference;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * This is the start up activity. Buttons for starting the main App Activity as well as buttons for
 * preferences are shown to the user on the screen.
 * 
 * @author TheHive
 */
public class StartUpActivity extends Activity
{
	public static final int TTS_DATA_CHECK_CODE = 8941562;

	private Button btnStart = null;
	private Button btnSettings = null;
	private Button btnExit1 = null;
	private Button btnExit = null;
	private Button btnInfo = null;
	
	private SharedPreferences preferences = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.startup);
		
		checkTTSDataAvailability(); //Check to TextToSpeech data
		
		btnStart = (Button) findViewById(R.id.btnStart);
		
		btnStart.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(StartUpActivity.this, EditOptionFileActivity.class);
				startActivity(intent);
			}
			
		});
		
		btnSettings = (Button) findViewById(R.id.btnSettings); //opens the app preference screen
		
		btnSettings.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(StartUpActivity.this, VizLabPreference.class);
				startActivity(intent);
			}
			
		});
		
		btnInfo = (Button) findViewById(R.id.btnAbout); //starts the About activity
		
		btnInfo.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(StartUpActivity.this, About.class);
				startActivity(intent);
			}
			
		});
		
		btnExit = (Button) findViewById(R.id.btnExit);
		
		btnExit.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				finish();
			}
			
		});
		
		btnExit1 = (Button) findViewById(R.id.btnExit1); //exit the activity
		
		btnExit1.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				finish();
			}
			
		});	
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		
		//Check if the server settings in the preference is empty or missing
		checkPreferences();
	}

	/**
	 * Execute to finish teh activity.
	 */
	@Override
	public void finish() 
	{
		super.finish();
	}

	/**
	 * Creates the options menu by inflating the given xml file.
	 */
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.optionsmenu, menu);
        return true;
    }
	
	/**
	 * Detect which item on the options menu was selected.
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId())
		{
		case R.id.about:
			startActivity(new Intent("com.androidvizlab.bachelor.About"));
			break;
		case R.id.preferences:
			Intent i = new Intent("com.androidvizlab.bachelor.utilities.VizLabPreference");
			startActivity(i);
			break;
		case R.id.exit:
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	//*** CHECK SAVED PREFERENCES ***//
	
	/**
	 * Use to check if the user has not yet filled the server settings or settings are missing in the preferences.
	 */
	public void checkPreferences()
	{
		preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		
		String serveraddress = preferences.getString("serveraddress", "not found");
		String brokeraddress = preferences.getString("brokeraddress","not found");
		int serverPort = NumberConverter.convertToInt(preferences.getString("portnumber", "not found"), -1);
		int brokerPort = NumberConverter.convertToInt(preferences.getString("brokerPortNumber", "not found"), -1);
		
		if(serveraddress.equals("not found") || brokeraddress.equals("not found") 
				|| serverPort < 0 || brokerPort < 0)
		{
			Intent i = new Intent("com.androidvizlab.bachelor.utilities.VizLabPreference");
			startActivity(i);
		}
	}
	
	//*** TEXT TO SPEECH ***//
	
	/**
	 * Start an activity for result to check if the Text to speech engine is available in the device.
	 * If Text to speech is not available, the user will be brought to the google play store download 
	 * the Text to speech engine.
	 */
	public void checkTTSDataAvailability()
	{
		Intent checkIntent = new Intent();
		checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
		startActivityForResult(checkIntent, TTS_DATA_CHECK_CODE);
	}

	/**
	 * When the result from the started activity is received. The requesCode will be compared to the 
	 * TTS data check code we provided and if it is a match the resultCode will be tested if the TextToSpeech data
	 * is available. Otherwise a request to install the TextToSpeech feature will be started.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		if(requestCode == TTS_DATA_CHECK_CODE)
		{
			if(resultCode != TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
				// missing data, install it
	            Intent installIntent = new Intent();
	            installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
	            startActivity(installIntent);
            }
		}
		
	}
}
